
/*=========================================
* Author: Administrator
* DateTime:2017/6/20 19:20:37
* Description:$safeprojectname$
==========================================*/

using System;
using System.Collections.Generic;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;

namespace OrderSystem
{
    public class ClientMediator : Mediator
    {
        private ClientProxy clientProxy = null;
        public new const string NAME = "ClientMediator";
        private ClientView View
        {
            get { return (ClientView)ViewComponent; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="view">关联的客户视图实例</param>
        public ClientMediator(ClientView view) : base(NAME, view)
        {
            view.CallWaiter += data => { SendNotification(OrderSystemEvent.CALL_WAITER, data); };
            view.Order += data => { SendNotification(OrderSystemEvent.ORDER, data); };
            //view.Pay += () => { SendNotification(OrderSystemEvent.PAY); };
        }

        public override void OnRegister()
        {
            base.OnRegister();
            clientProxy = Facade.RetrieveProxy(ClientProxy.NAME) as ClientProxy;
            if (null == clientProxy)
                throw new Exception("获取" + ClientProxy.NAME + "代理失败");
            IList<Action<object>> actionList = new List<Action<object>>()
            {
                item =>  SendNotification(OrderSystemEvent.GUEST_BE_AWAY, item, "Add"),
               // item => {SendNotification(OrderSystemEvent.ADD_GUEST,item);},
               item =>  SendNotification(OrderSystemEvent.HotelCommend, item,"SHANGCAI"),
                //item =>  SendNotification(OrderSystemEvent.GET_PAY, item),
             };
            View.UpdateClient(clientProxy.Clients, actionList);
        }

        /// <summary>
        /// 列出感兴趣的通知
        /// 定义当前中介者需要处理的系统通知类型
        /// </summary>
        /// <returns>通知名称列表</returns>
        public override IList<string> ListNotificationInterests()
        {
            IList<string> notifications = new List<string>();
            notifications.Add(OrderSystemEvent.CALL_WAITER);
            notifications.Add(OrderSystemEvent.ORDER);
            notifications.Add(OrderSystemEvent.PAY);
            notifications.Add(OrderSystemEvent.ADD_GUEST);
            return notifications;
        }

        /// <summary>
        /// 处理接收到的通知
        /// 根据通知类型执行相应的业务逻辑和视图更新
        /// </summary>
        /// <param name="notification">接收到的通知对象</param>
        public override void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                case OrderSystemEvent.CALL_WAITER:
                    ClientItem client = notification.Body as ClientItem;
                    //  SendNotification(OrderSystemEvent.CALL_WAITER,client);
                    break;
                case OrderSystemEvent.ORDER:
                    Order order1 = notification.Body as Order;
                    if (null == order1)
                        throw new Exception("order1 is null ,please check it!");
                    order1.client.state++;
                    View.UpdateState(order1.client);
                    break;
                case OrderSystemEvent.PAY:
                    // Order finishOrder = notification.Body as Order;
                    WaiterItem item = notification.Body as WaiterItem;
                    // if ( null == finishOrder )
                    //   throw new Exception("finishOrder is null ,please check it!");
                    View.UpdateState(item.order.client);
                    break;
                case OrderSystemEvent.ADD_GUEST:
                    //clientProxy = Facade.RetrieveProxy(ClientProxy.NAME) as ClientProxy;
                    ClientItem clientItem = notification.Body as ClientItem;
                    if (null == clientProxy)
                        throw new Exception("获取" + ClientProxy.NAME + "代理失败");
                    View.UpdateState(clientItem);
                    break;
            }
        }
    }
}